package com.atguigu.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * projectName: shf
 *
 * @author: cbc
 * time: 2022/6/14 10:11 周二
 * description:
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {

    @Reference
    private UserInfoService userInfoService;

    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request) {
        request.getSession().removeAttribute("USER");
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        String password = loginVo.getPassword();
        String phone = loginVo.getPhone();
        //参数效验
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(phone)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //通过手机号码进行查询对象
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (null == userInfo) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        if (!MD5.encrypt(password).equals(userInfo.getPassword())) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        if (userInfo.getStatus() == 0) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //存储用户的基本信息
        request.getSession().setAttribute("USER", userInfo);
        Map map = new HashMap<>();
        map.put("password", password);
        map.put("nickName", userInfo.getNickName());
        return Result.ok(map);
    }

    @RequestMapping("/register")
    public Result register(
            @RequestBody RegisterVo registerVo,
            HttpServletRequest request) {
        //获取昵称
        String nickName = registerVo.getNickName();
        //获取手机号码
        String phone = registerVo.getPhone();
        //获取密码
        String password = registerVo.getPassword();
        //获取验证码  用户输入
        String code = registerVo.getCode();

        //效验当前用户传递过来的数据是否有值
        if (StringUtils.isEmpty(nickName) || StringUtils.isEmpty(phone)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //效验验证码是否正确
        //从session里面获取验证码  手机发过来
        String code1 = (String) request.getSession().getAttribute("CODE");
        //判断
        if (!code.equals(code1)) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //根据手机号码查询是否有这个用户
        UserInfo userInfo = userInfoService.getByPhone(phone);
        //如果userInfo为null
        if (null != userInfo) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        userInfo = new UserInfo();
        userInfo.setNickName(nickName);
        userInfo.setPhone(phone);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setStatus(1);//1表示正常，0表示锁定；
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    @RequestMapping("/sendCode/{moble}")
    public Result sendCode(
            @PathVariable String moble,
            ModelMap modelMap,
            HttpServletRequest request) {
        String code = "1111";
        //存储到session
        request.getSession().setAttribute("CODE", code);
        return Result.ok(code);
    }
}
